<?php
/**
 * Created by PhpStorm.
 * User: yandian
 * Date: 2017/4/25
 * Time: 10:56
 */

namespace app\weixin\controller;


use app\common\controller\LiteBase;
use app\common\util\LoginManager;

class CoralReef extends LiteBase
{
    public function index()
    {
        $corals = \app\common\model\CoralReef::instance()
            ->where(['member_id' => LoginManager::getLoginUserId(), 'title' => ''])
            ->select();
        $this->assign('corals', $corals);
        return $this->fetch();
    }

    public function ranklist()
    {
        return $this->fetch();
    }

    public function rename()
    {
        if (!empty($_POST['names'])) {
            $names = $_POST['names'];
            if (!empty($names) && is_array($names)) {
                $member_id = LoginManager::getLoginUserId();
                $instance = \app\common\model\Coral::instance();
                foreach ($names as $id => $name) {
                    if ($instance->where(['id' => $id, 'member_id' => $member_id, 'title' => ''])->count()) {
                        $instance->where(['id' => $id])->save([
                            'title' => $name
                        ]);
                    }
                }
                return $this->jsonSuccess();
            }
        }
        return $this->jsonFail('您没有需要命名的珊瑚礁');
    }
}